/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import dao.ContestJpaController;
import entities.Contest;
import entities.Problem;
import entities.User;
import java.util.Date;
import java.util.List;

public class ContestService {
    private static ContestJpaController ctc;
    private static UserService us;
    private static ProblemService ps;

    private ContestJpaController getJPAController()
    {
        if(ctc == null)
            ctc = new ContestJpaController();
        return ctc;
    }

    private UserService getUserService()
    {
        if(us == null)
            us = new UserService();
        return us;
    }

    public boolean saveContest(String contestID, String name, Date startTime, Date endTime , String handle)
    {
        boolean ret = true;
        ctc = getJPAController();
        us = getUserService();
        User u = null;
        Contest c = new Contest(0, contestID, name, startTime, endTime);
        try
        {
            u = us.getUser(handle);
            System.out.println("UserHandle: " + u.getHandle());
            c.setUser(u);
            System.out.println("User: "+ c.getUser().getHandle() );
            ctc.create(c);
        }
        catch (Exception e)
        {
            ret = false;
        }
        return ret;
    }
    public Contest getContest(String contestID)
    {
        ctc = getJPAController();
        return ctc.getContestByID(contestID);
    }


    public boolean editContest(Contest c)
    {
        ctc = getJPAController();
        try
        {
            ctc.edit(c);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public boolean deleteContest(Contest c)
    {
        ctc = getJPAController();
        try
        {
            ctc.destroy(c.getIdContest());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public List<Contest> getAllContests()
    {
        ctc = getJPAController();
        return ctc.findAll();
    }

    public Contest getContest(int idContest)
    {
        ctc = getJPAController();
        return ctc.findContest(idContest);
    }

    private ProblemService getProblemService()
    {
        if (ps == null)
        {
            ps = new ProblemService();
        }
        return ps;
    }

    public boolean addProblem(Contest c, Problem p)
    {
        ps = getProblemService();
        ctc = getJPAController();
        c.getProblemCollection().add(p);
        p.getContestCollection().add(c);
        try
        {
            ctc.edit(c);
        } catch (Exception e)
        {
            return false;
        }
        return ps.editProblem(p);
    }

    public boolean removeProblem(Contest c, Problem p)
    {
        ps = getProblemService();
        ctc = getJPAController();
        c.getProblemCollection().remove(p);
        p.getContestCollection().remove(c);
        try
        {
            ctc.edit(c);
        } catch (Exception e)
        {
            return false;
        }
        return ps.editProblem(p);
    }

    public Contest getContestByName(String contestName)
    {
        ctc = getJPAController();
        try
        {
            return ctc.findContestByName(contestName);
        } catch (Exception e)
        {
            return null;
        }
    }


}
